package com.agadmin.manage.handle.software;

import com.agadmin.api.Api;
import com.agadmin.entity.software.Software;
import com.agadmin.manage.ManageApiHandle;
import com.agadmin.manage.vo.reponse.software.SoftwareGetListResponse;
import com.agadmin.manage.vo.request.software.SoftwareGetListRequest;
import com.agadmin.mapper.SoftwareMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@Api
public class SoftwareGetListManage implements ManageApiHandle<SoftwareGetListRequest, SoftwareGetListResponse> {

    private long total;

    @Autowired
    SoftwareMapper softwareMapper;

    @Override
    public SoftwareGetListResponse handler(SoftwareGetListRequest softwareGetListRequest) {

        List<Software> results = userSoftwareGetList(softwareGetListRequest);

        SoftwareGetListResponse res=new SoftwareGetListResponse();

        res.setTotalPage(total);
        res.setData(results);

        return res;
    }


    private List<Software> userSoftwareGetList(SoftwareGetListRequest req) {
        Page<Software> page = new Page<Software>(req.getCurrentPage(), req.getPageSize());

        LambdaQueryWrapper<Software> qw = new LambdaQueryWrapper<>();
        qw.like(req.getSoftwareName()!=null,Software::getSoftwareName,req.getSoftwareName());

        Page<Software> softwarePage = softwareMapper.selectPage(page, qw);

        total = softwarePage.getTotal();

        List<Software> results = softwarePage.getRecords();

        return results;
    }
}
